package com.coinland.study.util;

import android.content.Context;
import android.view.Gravity;
import android.widget.Toast;

/**
 * 类描述:
 * 创建人:  shenZ
 * 创建时间: 2019/7/22
 * 联系方式: 252079249@qq.com
 * 修改时间: 2019/7/22
 */
public class ToastUtil {

    /**
     * 展示
     *
     * @param text
     *            内容
     * @param duration
     *            时间
     * @param context
     */
    public static void show(CharSequence text, int duration, Context context) {
        Toast.makeText(context, text, duration).show();
    }

    /**
     * 展示
     *
     * @param resId
     *            资源id
     * @param duration
     *            时间
     * @param context
     */
    public static void show(int resId, int duration, Context context) {
        show(context.getText(resId), duration, context);
    }

    /**
     * 短时间展示(Toast.LENGTH_SHORT)
     *
     * @param text
     *            资源id
     * @param context
     */
    public static void showShort(CharSequence text, Context context) {
        show(text, Toast.LENGTH_SHORT, context);
    }

    /**
     * 长时间展示(Toast.LENGTH_LONG)
     *
     * @param text
     *            资源id
     * @param context
     */
    public static void showLong(CharSequence text, Context context) {
        show(text, Toast.LENGTH_LONG, context);
    }

    /**
     * 显示短提示框
     *
     * @param context
     *            上下文
     * @param resId
     *            资源ID
     * @param formatArgs
     *            参数
     */
    public final static Toast showShortToast(Context context, int resId,
                                             Object... formatArgs) {

        String msg = context.getString(resId, formatArgs);
        Toast toast = Toast.makeText(context, msg, Toast.LENGTH_SHORT);
        toast.setGravity(Gravity.CENTER, 0, 0);
        toast.show();
        return toast;
    }

    private static Toast toast = null;

    public static void showShortToastByString(Context context, String hint) {
        if (toast == null) {
            toast = Toast.makeText(context, hint, Toast.LENGTH_SHORT);
        } else {
            toast.setText(hint);
            toast.setDuration(Toast.LENGTH_SHORT);
        }

        toast.show();
    }

    public static void showLongToastByString(Context context, String hint) {
        if (toast == null) {
            toast = Toast.makeText(context, hint, Toast.LENGTH_LONG);
        } else {
            toast.setText(hint);
            toast.setDuration(Toast.LENGTH_LONG);
        }

        toast.show();
    }

}
